/*
 * RSSTreeNode.java
 *
 * Created on 2007��7��22��, ����10:00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.afaker.rss.gui.tree;

import com.afaker.rss.common.GeneralUtils;
import com.afaker.rss.context.ApplicationContext;
import com.afaker.rss.feed.biz.FeedFetcher;
import com.afaker.rss.feed.biz.ROMEFeedFetcher;
import com.afaker.rss.feed.Feed;
import com.afaker.rss.gui.action.MenuAction;
import com.afaker.rss.gui.table.XMLTableModel;
import com.sun.syndication.fetcher.FetcherException;
import com.sun.syndication.io.FeedException;
import com.afaker.rss.xml.XMLDocumentFactory;
import java.io.IOException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import org.jdom.Element;


public class RSSTreeNode extends DefaultMutableTreeNode{
    private String uri;
    private String title;
    private String feedURL;
    private Object userObject;
    private boolean isFeed;
    private int allEntries;
    private int unReaded;
    private int readed;
    private String feedName;
    /** Creates a new instance of RSSTreeNode */
    public RSSTreeNode() {
        super();
    }
    public RSSTreeNode(Object userObject) {
        super(userObject);
        this.userObject=userObject;
        if(userObject instanceof Element) {
            if((((Element)userObject)).getName().equals(MenuAction.CHANNEL_NODE_NAME)) {
                //setText(ele.getAttributeValue("title"));
                title=((Element)userObject).getAttributeValue(MenuAction.TITLE_ATTRIBUTE_NAME);
                feedName = ((Element)userObject).getAttributeValue(MenuAction.FEEDNAME_ATTRIBUTE_NAME);
                uri=((Element)userObject).getAttributeValue(MenuAction.URI_ATTRIBUTE_NAME);
                feedURL=((Element)userObject).getText();
                isFeed=true;
            } else {
                //setText(ele.getAttributeValue("name"));
                //setToolTipText(ele.)
                isFeed=false;
            }
        }
    }
    public String getFeedName() {
        return feedName;
    }
    public int getAllEntriesNumber() {
        return allEntries;
    }
    private  void getFeedEntryNumber() {
        FeedFetcher fetcher;
        Feed feed = null;
        try {
            fetcher=new ROMEFeedFetcher(GeneralUtils.getLocalChannelName(getTitle(),true,true));
            feed= fetcher.getFeed();
        } catch (FeedException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FetcherException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if(feed!=null) {
            allEntries = feed.getEntries().size();
        }
    }
    public void gainStatistics() {
        getFeedEntryNumber();
        gainReadedEntryNumber();
    }
    /**Get readed entries number*/
    private  void gainReadedEntryNumber() {
        XMLDocumentFactory xml=new XMLDocumentFactory();
        xml.build(GeneralUtils.getLocalChannelName(getFeedName(), false, true));
        Element root = xml.getRootElement();
        String n = root.getAttributeValue(XMLTableModel.NUMBER_ATTRIBUTE_NAME);
        readed = Integer.parseInt(n);
    }
    public boolean isFeed() {
        return isFeed;
    }
    /**Return the actual feed url*/
    public String getFeedURL() {
        return feedURL;
    }
    public int getUnReadedEntryNumber() {
        return allEntries - readed;
    }
    /*public void setUnReadedNumber(int num) {
        unReaded=num;
    }*/
    public String toString() {
        if(super.userObject instanceof Element) {
            Element ele=(Element)super.userObject;
            if(ele.isRootElement())
                return ApplicationContext.getApplicationContext().getProperty("category.root.name");
            if(ele.getName().equals(MenuAction.CHANNEL_NODE_NAME))
                return ele.getAttributeValue(MenuAction.TITLE_ATTRIBUTE_NAME);
            else
                return ele.getAttributeValue(MenuAction.FAVORITE_N_CATEGORY_NAME_ATTRIBUTE_NAME);
        }
        return super.toString();
    }
    public String getURI() {
        return uri;
    }
    /**Get user costom title,by default,it's the feed's name.*/
    public String getTitle() {
        return title;
    }
    public void insert(MutableTreeNode newChild, int childIndex) {
        super.insert(newChild,childIndex);
    }
}